package com.example.mes.security;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Configuration//告诉spring这是一个配置类
public class MyUserDetailService implements UserDetailsService {
    @Autowired
    private PasswordEncoder passwordEncoder;
    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
//        UserInfo userInfo = service.findUserByname(s);
//        if(userInfo==null)return null;
        //从数据库获取密码并加密
        String pwd = passwordEncoder.encode("123456");
        //用户认证
//如果有用户信息，返回一个user对象
        return new User("user",pwd,
                getCurrentUserAuthrities());
    }
    //获取当前用户拥有的权限
    public Collection<?extends GrantedAuthority>getCurrentUserAuthrities(){
        List<GrantedAuthority>list = new ArrayList<GrantedAuthority>();
        list.add(new SimpleGrantedAuthority("READ"));
        return list;
    }
}
